package com.example.timetable;

import android.app.AlertDialog;
import android.content.Context;

import android.content.SharedPreferences;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;



public class MyAlertDialog extends AlertDialog implements View.OnClickListener {

    private Button commit;
    private SharedPreferences preferences;
    private SharedPreferences.Editor editor;
    private TextView textView1;
    private TextView textView2;
    private EditText editText1;
    private EditText editText2;
    private Context context;
    String deletekey=null;
    protected MyAlertDialog(Context context,String fileName) {
        super(context);
        this.context=context;
        textView1=new TextView(context);
        textView2=new TextView(context);
        editText1=new EditText(context);
        editText2=new EditText(context);
        commit=new Button(context);
        commit.setOnClickListener(this);
        preferences=context.getSharedPreferences(fileName,Context.MODE_PRIVATE);
        editor=preferences.edit();
        initDialog(context);
    }

    protected void initDialog(Context context){
        LinearLayout linearLayout=new LinearLayout(context);
        LinearLayout linearLayout1=new LinearLayout(context);
        LinearLayout linearLayout2=new LinearLayout(context);

        commit.setText("提交");

        linearLayout1.addView(textView1);
        linearLayout1.addView(editText1);
        linearLayout2.addView(textView2);
        linearLayout2.addView(editText2);
        linearLayout.addView(linearLayout1);
        linearLayout.addView(linearLayout2);
        linearLayout.addView(commit);

        linearLayout.setOrientation(LinearLayout.VERTICAL);
        linearLayout1.setOrientation(LinearLayout.HORIZONTAL);
        linearLayout2.setOrientation(LinearLayout.HORIZONTAL);

        LinearLayout.LayoutParams linearParams=(LinearLayout.LayoutParams)editText1.getLayoutParams();
        linearParams.width=800;
        editText1.setLayoutParams(linearParams);
        editText2.setLayoutParams(linearParams);

        LinearLayout.LayoutParams buttonParams=(LinearLayout.LayoutParams)commit.getLayoutParams();
        buttonParams.gravity= Gravity.CENTER;
        commit.setLayoutParams(buttonParams);
        setView(linearLayout);
    }
    public void setDialog(String title,String text1,String text2){
        setTitle(title);
        textView1.setText(text1);
        textView2.setText(text2);
    }
    @Override
    public void onClick(View v) {
        String edit1=editText1.getText().toString();
        String edit2=editText2.getText().toString();
        editor.putString(edit1,edit2);
        if(deletekey!=null)
            editor.remove(deletekey);
        editor.commit();
        dismiss();
    }
    public void deleteItem(String key){
       deletekey=key;
    }
}
